package com.itita.ww2;

import com.itita.ww2.core.GlobalData;
import com.itita.ww2.core.ServerService;
import com.itita.ww2.core.WW2ShutDownHook;
import com.itita.ww2.handler.eventhandler.OnUserDisconnectHandler;
import com.itita.ww2.handler.eventhandler.OnUserJoinZoneHandler;
import com.itita.ww2.handler.eventhandler.OnUserLogoutHandler;
import com.itita.ww2.handler.requesthandler.account.AccountHandler;
import com.itita.ww2.handler.requesthandler.account.LoginComponent;
import com.itita.ww2.handler.requesthandler.alliance.AllianceHandler;
import com.itita.ww2.handler.requesthandler.army.ArmyHandler;
import com.itita.ww2.handler.requesthandler.building.BuildingHandler;
import com.itita.ww2.handler.requesthandler.chat.ChatHandler;
import com.itita.ww2.handler.requesthandler.cityconstruction.CityConstructionHandler;
import com.itita.ww2.handler.requesthandler.game.GameHandler;
import com.itita.ww2.handler.requesthandler.item.ItemHandler;
import com.itita.ww2.handler.requesthandler.lottery.LotteryHandler;
import com.itita.ww2.handler.requesthandler.mission.MissionHandler;
import com.itita.ww2.handler.requesthandler.pay.PayHandler;
import com.itita.ww2.handler.requesthandler.shop.ShopHandler;
import com.itita.ww2.handler.requesthandler.user.UserHandler;
import com.itita.ww2.handler.requesthandler.worldmap.WorldMapHandler;
import com.itita.ww2.utils.WebProxyImpl;
import com.smartfoxserver.v2.SmartFoxServer;
import com.smartfoxserver.v2.core.SFSEventType;
import com.smartfoxserver.v2.extensions.ExtensionLogLevel;
import com.smartfoxserver.v2.extensions.SFSExtension;

public class WW2Extension extends SFSExtension {
	private LoginComponent lc;

	@Override
	public void init() {
		/*
		 * 初始化配置
		 */
		try {
			GlobalData.load(getCurrentFolder(), true);
		} catch (Exception e) {
			trace(ExtensionLogLevel.ERROR, "[GlobalData] - ", e.getMessage());
		}

		ServerService.getInstance().init(this, SmartFoxServer.getInstance().getTaskScheduler()); // 服务器数据初始化
		/*
		 * WebProxy
		 */
		this.getParentZone().setProperty("webProxy", new WebProxyImpl());

		/*
		 * Client events
		 */
		//
		addRequestHandler(AccountHandler.COMMAND_PREFIX, new AccountHandler()); // 注册
		lc = new LoginComponent(this); // 登录
		addRequestHandler(ChatHandler.COMMAND_PREFIX, new ChatHandler()); // 聊天
		addRequestHandler(GameHandler.COMMAND_PREFIX, new GameHandler()); // 游戏数据
		addRequestHandler(UserHandler.COMMAND_PREFIX, new UserHandler()); // User
		addRequestHandler(PayHandler.COMMAND_PREFIX, new PayHandler()); // 支付
		addRequestHandler(ShopHandler.COMMAND_PREFIX, new ShopHandler()); // 商店
		addRequestHandler(ItemHandler.COMMAND_PREFIX, new ItemHandler()); // 道具系统
		addRequestHandler(ArmyHandler.COMMAND_PREFIX, new ArmyHandler()); // 兵
		addRequestHandler(BuildingHandler.COMMAND_PREFIX, new BuildingHandler()); // 建筑
		addRequestHandler(MissionHandler.COMMAND_PREFIX, new MissionHandler()); // 任务
		// addRequestHandler(BaseMapHandler.COMMAND_PREFIX, new BaseMapHandler()); // 用户基地地图操作
		// addRequestHandler(AggressionHandler.COMMAND_PREFIX, new AggressionHandler()); // 偷袭战
		addRequestHandler(AllianceHandler.COMMAND_PREFIX, new AllianceHandler()); // 联盟
		//		addRequestHandler(AllianceBattleHandler.COMMAND_PREFIX, new AllianceBattleHandler()); // 盟战
		addRequestHandler(WorldMapHandler.COMMAND_PREFIX, new WorldMapHandler()); // 大地图
		addRequestHandler(CityConstructionHandler.COMMAND_PREFIX, new CityConstructionHandler()); // 城池建设
		addRequestHandler(LotteryHandler.COMMAND_PREFIX, new LotteryHandler()); // 抽奖系统

		/*
		 * Server events
		 */
		addEventHandler(SFSEventType.USER_DISCONNECT, OnUserDisconnectHandler.class);
		addEventHandler(SFSEventType.USER_JOIN_ZONE, OnUserJoinZoneHandler.class);
		addEventHandler(SFSEventType.USER_LOGOUT, OnUserLogoutHandler.class);

		Runtime.getRuntime().addShutdownHook(new WW2ShutDownHook());
	}

	@Override
	public void destroy() {
		super.destroy();
		lc.destroy();
	}
}
